package com.agileai.wm.module.daywork.handler;

import java.util.Date;
import java.util.List;
import java.text.SimpleDateFormat;
import com.agileai.util.*;
import com.agileai.wm.module.daywork.service.WmDayworkManage;
import com.agileai.domain.*;
import com.agileai.hotweb.domain.*;
import com.agileai.hotweb.domain.core.User;
import com.agileai.hotweb.renders.LocalRenderer;
import com.agileai.hotweb.renders.ViewRenderer;
import com.agileai.hotweb.annotation.PageAction;
import com.agileai.hotweb.controller.core.SimpleHandler;

/**
 * generated by miscdp
 */
public class DayCheckHandler extends SimpleHandler{
	public DayCheckHandler(){
		super();
	}
	public ViewRenderer prepareDisplay(DataParam param) {
		this.setAttributes(param);
		String operaType = param.get(OperaType.KEY);
		this.setOperaType(operaType);
		
		SimpleDateFormat currentTime = new SimpleDateFormat("yyyy-MM-dd");
		String currentDate = currentTime.format(new Date());
		DataParam wtParam = new DataParam();
		if(param.get("TW_TIME")!=null){
			String day=param.get("TW_TIME");
			wtParam.put("TW_TIME",day);
		}
		else{
			wtParam.put("TW_TIME",currentDate);
		}

		
		User user = (User)this.getUser();
		String userId = user.getUserId();
		List<DataRow> groupRecords = getService().getMngGroupRecords(userId);
		String grpId = param.get("grpId");
		//USER_ID
		String currentUserId = param.get("currentUserId");
		
		if(groupRecords != null && groupRecords.size() > 0){
			if (StringUtil.isNullOrEmpty(grpId)){
				DataRow row = groupRecords.get(0);
				grpId = row.getString("GRP_ID");
			}
			
			FormSelect grpFormSelect = new FormSelect();
			grpFormSelect.setKeyColumnName("GRP_ID");
			grpFormSelect.setValueColumnName("GRP_NAME");
			grpFormSelect.putValues(groupRecords);
			grpFormSelect.setSelectedValue(grpId);
			this.setAttribute("grpId", grpFormSelect);
			
			List<DataRow> empRecords = getService().queryDayEmpRecords(grpId);
			
			if (empRecords != null && empRecords.size() > 0){
				if (StringUtil.isNullOrEmpty(currentUserId)){
					DataRow userRow = empRecords.get(0);
					currentUserId = userRow.getString("USER_ID");					
				}
			}
			
			int markId = 0;
			for(int i=0; i<empRecords.size(); i++){
				DataRow userRow = empRecords.get(i);
				String userIdNum = userRow.getString("USER_ID");
				if(userIdNum.equals(currentUserId)){
					markId = i;
				}
			}
			String currentTabId = param.get("currentTabId");
			if(null == currentTabId || "null".equals(currentTabId)){
				currentTabId = "0";
			}
			if(markId == 0 || "0" == currentTabId || "0".equals(currentTabId)){
				DataRow userRow = empRecords.get(0);
				currentUserId = userRow.getString("USER_ID");
				currentTabId = "0";
			}else{
				currentTabId = String.valueOf(markId);
			}
			
			this.setAttribute("empRecords",empRecords);		
//			this.setAttribute("currentTabId", getAttribute("currentTabId",0));
			this.setAttribute("currentTabId", currentTabId);
			
			processPageAttributes(param);
		}
		this.setAttribute("currentUserId", currentUserId);
		return new LocalRenderer(getPage());
	}
	
	protected void processPageAttributes(DataParam param){
		SimpleDateFormat currentTime = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
		String currentDate = null;
		if(param.get("TW_TIME") != null){
			String day = param.get("TW_TIME");
			currentDate = day;
		}
		else{
			currentDate = currentTime.format(new Date());// new Date()为获取当前系统时间
		}
		this.setAttribute("TW_TIME", currentDate);
	}
	
	@PageAction
	public ViewRenderer beforeDay(DataParam param){
		String currentDay = param.get("TW_TIME");
		String row = getService().getBeforeDayRow(currentDay);
		if (row != null && row.length() > 0){
			String day = row;
			param.put("TW_TIME",day);
		}else{
			this.setErrorMsg("这是第一天日报记录 !!!");
		}
		return prepareDisplay(param);
	}
	
	@PageAction
	public ViewRenderer nextDay(DataParam param){
		String currentDay = param.get("TW_TIME");
		String row = getService().getNextDayRow(currentDay);
		if (row != null && row.length() > 0){
			String day = row;
			param.put("TW_TIME",day);
		}else{
			this.setErrorMsg("这是最后一天日报记录 !!!");
		}
		return prepareDisplay(param);
	}
	
	
	@PageAction
	public ViewRenderer currentDay(DataParam param){
		String currentDay = param.get("TW_TIME");
		String row = getService().retrieveCurrentDayRow(currentDay );
		param.put("TW_TIME", row);
		return prepareDisplay(param);
	}
	protected WmDayworkManage getService() {
        return (WmDayworkManage) this.lookupService(WmDayworkManage.class);
    }
}
